package com.example.lotterysystem.service.activitystatus.operator;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum prizeTargetStatus = convertActivityStatusDTO.getPrizeTargetStatus();
        Long activityId = convertActivityStatusDTO.getActivityId();
        if(null == prizeId || null==prizeTargetStatus || null ==prizeTargetStatus) {
            return false;
        }
        ActivityPrizeDO apDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        if(null == apDO) {
            return false;
        }
        //当前状态与⽬标状态不⼀致才更新，这⾥可以再继续校验状态在何种状态下可以扭转
        if(prizeTargetStatus.name().equalsIgnoreCase(apDO.getStatus())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getPrizeId(),
                    convertActivityStatusDTO.getPrizeTargetStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
